<template>
  <div class="app-container">
    <div class="space">
      <el-form
        v-show="showSearch"
        ref="queryForm"
        :model="queryParams"
        size="small"
        :inline="true"
      >
        <el-form-item label="名称" prop="shopName">
          <el-input
            v-model="queryParams.shopName"
            placeholder="通过门店名称进行模糊搜索"
            clearable
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="修改时间" prop="signTime">
          <el-date-picker
            v-model="queryParams.signTime"
            :default-time="['00:00:00', '23:59:59']"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="pickerOptions"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh" class="delBtn" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-if="addMenu"
            >新增门店</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :show-search.sync="showSearch"
        :columns="columns"
        @queryTable="getListData"
      />
    </el-row>
    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="roleList"
      style="margin-top: 20px"
      @row-click="peopleManege"
    >
      <el-table-column
        v-if="columns[0].visible"
        key="id"
        label="门店ID"
        prop="id"
        :show-overflow-tooltip="true"
        align="left"
      />
      <el-table-column
        v-if="columns[1].visible"
        key="name"
        label="门店名称"
        prop="name"
        :show-overflow-tooltip="true"
        align="left"
      />
      <el-table-column
        v-if="columns[2].visible"
        key="userName"
        label="创建人"
        prop="userName"
        :show-overflow-tooltip="true"
        align="left"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.userName + "-" + scope.row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columns[3].visible"
        key="address"
        label="门店地址"
        prop="address"
        :show-overflow-tooltip="true"
        align="left"
      />
      <el-table-column
        v-if="columns[4].visible"
        key="addressDetail"
        label="详细地址"
        prop="addressDetail"
        :show-overflow-tooltip="true"
        align="left"
      />
      <el-table-column
        v-if="columns[5].visible"
        key="mobile"
        label="联系方式"
        prop="mobile"
        :show-overflow-tooltip="true"
        align="left"
      />
      <el-table-column
        v-if="columns[6].visible"
        key="updateTime"
        label="最近修改时间"
        align="left"
        prop="updateTime"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="left"
        fixed="right"
        class-name="fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            class="btnText"
            @click.stop="peopleManege(scope.row)"
            v-if="detailMenu"
            >门店人员管理</el-button
          >
          <el-button
            type="text"
            class="btnText"
            @click.stop="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            type="text"
            class="btnText"
            @click.stop="handleUpdate(scope.row)"
            v-if="editMenu"
            >修改</el-button
          >
          <!-- <el-button
            class="btnText"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            >拒绝</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getListData"
    />
    <el-dialog
      :title="title + '门店'"
      :visible.sync="open"
      width="786px"
      append-to-body
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="addRules"
        label-width="100px"
      >
        <el-form-item label="门店名称" prop="name">
          <el-input
            v-model="addForm.name"
            placeholder="请输入门店名称"
            maxlength="15"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="门店地址" prop="address">
          <el-cascader
            :options="options"
            style="width: 100%"
            :props="{ value: 'name', label: 'name' }"
            v-model="addForm.address"
            @change="changeAddress"
            placeholder="请输入门店地址"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input
            v-model="addForm.addressDetail"
            placeholder="请输入详细地址"
          />
        </el-form-item>
        <el-form-item label="联系方式" prop="mobile">
          <el-input v-model="addForm.mobile" placeholder="请输入门店联系方式" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button
          type="primary"
          :loading="loading"
          class="add"
          @click="submitForm"
          >{{ addForm.id ? "确认修改" : "确认新增" }}</el-button
        >
      </div>
    </el-dialog>
    <drawer-vue :row="row" :drawer="drawer" @handleClose="drawer = false" />
  </div>
</template>

<script>
import { getShopList, handShopData, delShop } from "@/api/index";
import drawerVue from "./drawer.vue";
import { mapGetters } from "vuex";
import addressJson from "@/assets/pcas-code.json";
export default {
  name: "ShopManage",
  components: { drawerVue },
  computed: {
    ...mapGetters(["menuIds"]),
    addMenu() {
      return this.menuIds.includes("200");
    },
    detailMenu() {
      return this.menuIds.includes("201");
    },
    editMenu() {
      return this.menuIds.includes("202");
    },
  },
  data() {
    return {
      options: addressJson,
      row: {},
      drawer: false,
      loading: false,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      open: false,
      columns: [
        { key: 0, label: `门店ID`, visible: true },
        { key: 1, label: `门店名称`, visible: true },
        { key: 2, label: `创建人`, visible: true },
        { key: 3, label: `门店地址`, visible: true },
        { key: 4, label: `详细地址`, visible: true },
        { key: 5, label: `联系方式`, visible: true },
        { key: 6, label: `最近修改时间`, visible: true },
      ],
      title: "新增",
      tableKey: 0,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 10,
      // 角色表格数据
      roleList: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        shopName: "",
        signTime: "",
      },
      // 表单参数
      form: {},
      addForm: {
        name: "",
        address: "",
        mobile: "",
        addressDetail: "",
      },
      addRules: {
        name: [
          { required: true, message: "门店名称不能为空", trigger: "blur" },
        ],
        address: [
          {
            required: true,
            message: "门店地址不能为空",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          { required: true, message: "门店联系方式不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getListData();
    console.log(this.menuIds);
  },
  methods: {
    changeAddress(areaName) {
      console.log(areaName);
    },
    /** 查询角色列表 */
    getListData() {
      const data = {
        ...this.queryParams,
        startTime:
          this.queryParams.signTime &&
          new Date(this.queryParams.signTime[0]).getTime(),
        endTime:
          this.queryParams.signTime &&
          new Date(this.queryParams.signTime[1]).getTime(),
      };
      delete data.signTime;
      getShopList(data).then((response) => {
        this.roleList = response.data.records;
        this.total = response.data.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.$nextTick(() => {
        this.queryParams.current = 1;
        this.getListData();
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.current = 1;
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let roleIds = row.id;
      if (Array.isArray(roleIds)) {
        roleIds = roleIds.join(",");
      }
      this.$modal
        .confirm("是否删除该门店？")
        .then(() => {
          return delShop({
            id: roleIds,
          });
        })
        .then(() => {
          this.getListData();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {});
    },
    /** 添加按钮操作 */
    handleAdd() {
      this.title = "新增";
      this.open = true;
      this.reset();
    },
    // 表单重置
    reset() {
      this.addForm = {
        name: "",
        id: "",
        address: "",
        mobile: "",
        addressDetail: "",
      };
      this.fileList = [];
      this.resetForm("addForm");
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    peopleManege(row) {
      if (this.detailMenu) {
        this.row = row;
        this.drawer = true;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "修改";
      this.addForm.id = row.id;
      this.addForm.name = row.name;
      this.addForm.mobile = row.mobile;
      this.addForm.addressDetail = row.addressDetail || "";
      this.addForm.address = row.address && row.address.split("/");
      this.open = true;
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          handShopData({
            ...this.addForm,
            address: this.addForm.address.join("/"),
            userId: localStorage.getItem("userId"),
          })
            .then((response) => {
              this.$modal.msgSuccess(this.addForm.id ? "修改成功" : "添加成功");
              this.open = false;
              this.loading = false;
              this.getListData();
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/role/export",
        {
          ...this.queryParams,
        },
        `role_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.btnText {
  color: #0052d9 !important;
}
.dialogForm {
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep.el-form--label-top .el-form-item__label {
    padding: 0;
  }
}
.delBtn {
  background: #e7e7e7;
  color: #000;
  border: none;
}
.open {
  width: 52px;
  height: 32px;
  background: #e3f9e9;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #2ba471;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 400;
  font-size: 14px;
  color: #2ba471;
}
.close {
  width: 52px;
  height: 32px;
  background: #d8d8d8;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #767676;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 400;
  font-size: 14px;
  color: #767676;
}
.statusBox {
  width: 100%;
  display: flex;
  justify-content: center;
}
.img {
  width: 38px;
  height: 38px;
}
</style>
